<?php

namespace App\Http\Livewire;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Livewire\Component;

class UserRegistration extends Component
{
    public $user_name;
    public $password;
    public $email;

    protected $rules = [
        'email' => 'bail|required|email|unique:App\Models\User,email',
        'user_name' => 'bail|required|unique:App\Models\User,name|max:30|alpha_dash',
        'password' => 'bail|required|min:8' //password
    ];


    public function updated($propertyName)
    {
        $this->validateOnly($propertyName);
    }

    public function registerUser()
    {
        $validatedData = $this->validate();

        $validatedData['password'] = Hash::make($validatedData['password']);

        $user = \App\Models\User::create($validatedData);

        $user->profile()->create();

        Auth::login($user);

        return redirect()->route('user-profile', ['userName' => $user->user_name]);
    }

    public function render()
    {
        return view('livewire.user.user-registration');
    }
}
